package com.itcast.activiti.controller;

import cn.itcast.ic.common.bean.Response;
import com.itcast.activiti.constant.NodeKeyTag;
import com.itcast.activiti.domain.ApproveDefinitionSave;
import com.itcast.activiti.entity.ApproveDefinition;
import com.itcast.activiti.entity.ApproveDefinitionHistory;
import com.itcast.activiti.service.IApproveDefinitionHistoryService;
import com.itcast.activiti.service.IApproveDefinitionService;
import com.itcast.activiti.service.IDeployRepositoryService;
import com.itcast.activiti.utils.ActivitiPage;
import com.itcast.activiti.utils.ItcastPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 部署流程、删除流程
 */
@RestController
@RequestMapping("/deploy")
public class DeployController {
    @Autowired
    private IDeployRepositoryService IDeployRepositoryService;
    @Autowired
    private IApproveDefinitionService iApproveDefinitionService;
    @Autowired
    private IApproveDefinitionHistoryService iApproveDefinitionHistoryService;

    /**
     * 验证不是流程key是否存在
     *
     * @param processDefinitionKey
     * @return
     */
    @GetMapping(value = "/check/{processDefinitionKey}")
    public Response checkProcessDefinitionKey(@PathVariable("processDefinitionKey") String processDefinitionKey) {
        return Response.success(IDeployRepositoryService.checkProcessDefinitionKey(processDefinitionKey));
    }

    /**
     * 获取部署流程定义分页数据
     *
     * @return
     */
    @PostMapping(value = "/process")
    public Response listProcessDefinitionPage(@RequestBody ActivitiPage activitiPage) {
        return Response.success(IDeployRepositoryService.listProcessDefinitionPage(activitiPage));
    }

    /**
     * 上传bpmn并部署
     *
     * @param file
     * @param key
     * @return
     */
    @PostMapping(path = "/file")
    public Response<Map<String, Object>> createDeploymentZIP(@RequestParam("file") MultipartFile file, @RequestParam(required = false) String key, @RequestParam(required = false) String tenantId) {
        return IDeployRepositoryService.createDeploymentFile(file, key, StringUtils.hasText(tenantId) ? tenantId : "ITCAST_PMIS");
    }


    /**
     * 新增、修改：approve_definition：审批定义表
     *
     * @param in
     * @return
     */
    @PostMapping("/json")
    public Response<ApproveDefinitionSave> saveApproveDefinition(@RequestBody ApproveDefinitionSave in) {
        return Response.success(iApproveDefinitionService.saveApproveDefinitionReturnSave(in));
    }

    /**
     * 获取bpnm文件
     *
     * @param deploymentId
     * @param processDefinitionId
     * @param processDefinitionKey
     * @param response
     */
    @GetMapping(path = "/bpmn")
    public void getBpmnFile(@RequestParam(required = false) String deploymentId, @RequestParam(required = false) String processDefinitionId, @RequestParam(required = false) String processDefinitionKey, HttpServletResponse response) {
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        IDeployRepositoryService.getBpmnFile(deploymentId, processDefinitionId, processDefinitionKey, outputStream);
    }

    /**
     * 审批定义列表
     *
     * @return
     */
    @PostMapping(value = "/definition")
    public Response listApproveDefinitionPage(@RequestBody ItcastPage itcastPage) {
        return Response.success(iApproveDefinitionService.listApproveDefinitionPage(itcastPage));
    }

    /**
     * 查询审批定义详情
     *
     * @return
     */
    @GetMapping(value = "/definition/{deploymentId}")
    public Response getApproveDefinition(@PathVariable("deploymentId") String deploymentId) {
        ApproveDefinitionHistory approveDefinition = iApproveDefinitionHistoryService.getApproveDefinitionHistory(deploymentId);
        return Response.success(approveDefinition);
    }
}
